<?php

class AppController extends Controller {
	
	var $components = array('Auth', 'Session');
	var $uses = array('Project', 'Task', 'ProfileParametr', 'Document');

	function beforeFilter()
	{
		parent::beforeFilter();
		
		$current_user = $this->Auth->user();
		
		if (empty($current_user))
			return;
		
		if (!$this->check_permissions_for_project($current_user, $this->get_profile_parametr('selected_project')))
		{
			$this->set_profile_parametr('selected_project', 0);
		}
		
		$this->current_project = $this->Project->read(null, $this->get_profile_parametr('selected_project'));
		
		if (isset($this->params['admin']) && $this->params['admin'])
		{
			$this->layout = 'admin';
		}
		
		if (preg_match('/(\btasks\b)|(\bdocuments\b)/', $this->params['controller']))
		{
			$this->layout = 'tasks';
			$this->set('projects', $this->Project->get_user_projects($current_user));
		}
		
		$this->Document->recursive = -1;
		$documents_blocked = $this->Document->find('all', array('conditions' => array(
			'Document.blocked >' => 0,
			'Document.modified <=' => date('Y-m-d H:i:s', time() - 60*60)
		)));
		$this->Document->recursive = 0;
		foreach ($documents_blocked as $document)
		{
			$document['Document']['blocked'] = 0;
			$this->Document->save($document, array('fieldList' => array('id', 'blocked')));
		}
		
		$this->set('current_user', $current_user);
		$this->set('current_project', $this->current_project);
		
	}
	
	function check_permissions_for_task($user, $task_id)
	{
		if ($user['User']['role'] == 'admin')
		{
			return true;
		}
		
		$user_projects = $this->Project->get_user_projects_list($user);
		
		$this->Task->recursive = -1;
		$task = $this->Task->find('first', array('conditions' => array('Task.id' => $task_id)));
		
		if (empty($user_projects) || empty($task))
		{
			return false;
		}
		else
		{
			return in_array($task['Task']['project_id'], array_keys($user_projects));
		}
	}
	
	function check_permissions_for_project($user, $project_id)
	{
		if ($user['User']['role'] == 'admin')
		{
			return true;
		}
		
		$user_projects = $this->Project->get_user_projects_list($user);
		return in_array($project_id, array_keys($user_projects));
	}
	
	protected function get_order_for_tasks()
	{
		$sort = (!empty($this->params['named']['sort_by'])) ? $this->params['named']['sort_by'] : '';
		
		$sort = explode('_', $sort);
		$order = '';
		if (count($sort) == 2)
		{
			switch($sort[0])
			{
				case 'priority' :
				{
					$order = 'Task.priority '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'name' :
				{
					$order = 'Task.name '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'project' :
				{
					$order = 'Project.name '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'status' :
				{
					$order = 'Task.status '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'performer' :
				{
					$order = 'Performer.username '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'modified' :
				{
					$order = 'Task.modified '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
			}
		}
		else
		{
			$order = 'Task.modified DESC';
		}
		
		return $order;
	}
	
	protected function get_order_for_documents()
	{
		$sort = (!empty($this->params['named']['sort_by'])) ? $this->params['named']['sort_by'] : '';
		
		$sort = explode('_', $sort);
		$order = '';
		if (count($sort) == 2)
		{
			switch($sort[0])
			{
				case 'name' :
				{
					$order = 'Document.name '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'creator' :
				{
					$order = 'Creator.username '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
				case 'modified' :
				{
					$order = 'Document.modified '.(($sort[1] == 'asc') ? 'asc' : 'desc');
					break;
				}
			}
		}
		else
		{
			$order = 'Document.modified DESC';
		}
		
		return $order;
	}
	
	function get_profile_parametr($parametr_name)
	{
		$profile_parametr = $this->ProfileParametr->find('first', array('conditions' => array('user_id' => $this->Auth->user('id'))));
		
		$profile_parametr = unserialize($profile_parametr['ProfileParametr']['data']);
		
		return (empty($profile_parametr[$parametr_name])) ? null : $profile_parametr[$parametr_name];
	}
	
	function set_profile_parametr($parametr_name, $parametr_value)
	{
		$profile_parametr = $this->ProfileParametr->find('first', array('conditions' => array('user_id' => $this->Auth->user('id'))));
		
		$profile_parametr['ProfileParametr']['data'] = unserialize($profile_parametr['ProfileParametr']['data']);
		
		$profile_parametr['ProfileParametr']['data'][$parametr_name] = $parametr_value;
		
		$profile_parametr['ProfileParametr']['data'] = serialize($profile_parametr['ProfileParametr']['data']);
		
		$this->ProfileParametr->save($profile_parametr);
	}
	
}